<?php

function apiGetmatch($db, $id) {
	$data = Array();
	$query = 'SELECT * FROM `match`  WHERE id = :id;';
	try {
		$statement = $db -> prepare($query);
		$statement -> bindParam(':id', $id, PDO::PARAM_INT);
		$statement -> execute();
		$result = $statement -> fetch(PDO::FETCH_ASSOC);
		if (!empty($result)) {
			// Check should have a new single user
			foreach ($result as $key => $value) {
				$data[$key] = $value;
			}
			
		} else {
			// User not found
			$output['code'] = 404;
			$output['status'] = 'fail';
			$output['message'] = $GLOBALS['messages'][60039];
			return $output;
		}

		$output['code'] = 200;
		$output['status'] = 'success';
		$output['message'] = $GLOBALS['messages'][60040];
		$output['data'] = $data;
	} catch (Exception $e) {
		$output['code'] = 500;
		$output['status'] = 'fail';
		$output['message'] = $GLOBALS['messages'][90003];
	}
	//$output = apiAddFolder($db, $p['username'], $p['username'], '/', 'admin');
	return $output;
}
function apiGetmatchbyname($db, $id) {
	$uname=htmlentities($id);
	$data = Array();
	$query = 'SELECT * FROM `match`  WHERE name = :name;';
	try {
		$statement = $db -> prepare($query);
		$statement -> bindParam(':name', $uname, PDO::PARAM_STR);
		$statement -> execute();
		$result = $statement -> fetch(PDO::FETCH_ASSOC);
		if (!empty($result)) {
			// Check should have a new single user
			foreach ($result as $key => $value) {
				$data[$key] = $value;
			}
			
		} else {
			// User not found
			$output['code'] = 404;
			$output['status'] = 'fail';
			$output['message'] = $GLOBALS['messages'][60039];
			return $output;
		}

		$output['code'] = 200;
		$output['status'] = 'success';
		$output['message'] = $GLOBALS['messages'][60040];
		$output['data'] = $data;
	} catch (Exception $e) {
		$output['code'] = 500;
		$output['status'] = 'fail';
		$output['message'] = $GLOBALS['messages'][90003];
	}
	//$output = apiAddFolder($db, $p['username'], $p['username'], '/', 'admin');
	return $output;
}
/**
 * Function to edit a UNetLab users.
 *
 * @param	PDO		$db					PDO object for database connection
 * @return  Array                       UNetLab users (JSend data)
 */
function apiGetmatchs($db) {
	$data = Array();

	$query = 'SELECT * FROM `match` ORDER BY id ASC;';
	try {
		$statement = $db -> prepare($query);
		$statement -> execute();
		while ($row = $statement -> fetch(PDO::FETCH_ASSOC)) {
			$data[$row['id']] = Array();
			$data[$row['id']]['id'] = $row['id'];
			$data[$row['id']]['name'] = $row['name'];
			$data[$row['id']]['didian'] = $row['didian'];
			$data[$row['id']]['leixin'] = $row['leixin'];
			$data[$row['id']]['lianxiren'] = $row['lianxiren'];
			$data[$row['id']]['bstime'] = $row['bstime'];
			$data[$row['id']]['beizhu'] = $row['beizhu'];
			
			$data[$row['id']]['sdate'] = $row['sdate'];
			$data[$row['id']]['cdate'] = $row['cdate'];
		}
			$output['code'] = 200;
			$output['status'] = 'success';
			$output['message'] = $GLOBALS['messages'][60040];
			$output['data'] = $data;
	} catch (Exception $e) {
		$output['code'] = 500;
		$output['status'] = 'fail';
		$output['message'] = $GLOBALS['messages'][90003];
	}
	return $output;
}

/**
 * Function to delete a UNetLab user.
 *
 * @param	PDO		$db					PDO object for database connection
 * @param	string	$user				UNetLab user
 * @return  Array                       Return code (JSend data)
 */
function apiDeletematch($db, $user) {
	if (empty($user)) {
		// User not found
		$output['code'] = 404;
		$output['status'] = 'fail';
		$output['message'] = $GLOBALS['messages'][60039];
		return $output;
	}

	try {
		// Free previously used pod
		$query = 'DELETE FROM matchreferee WHERE matchid = :id;';
		$statement = $db -> prepare($query);
		$statement -> bindParam(':id', $user, PDO::PARAM_INT);
		$statement -> execute();
		$query = 'DELETE FROM matchplayer WHERE matchid = :id;';
		$statement = $db -> prepare($query);
		$statement -> bindParam(':id', $user, PDO::PARAM_INT);
		$statement -> execute();
		$query = 'DELETE FROM `match` WHERE id = :id;';
		$statement = $db -> prepare($query);
		$statement -> bindParam(':id', $user, PDO::PARAM_INT);
		$statement -> execute();
		//$result = $statement -> fetch();


		$output['code'] = 201;
		$output['status'] = 'success';
		$output['message'] = $GLOBALS['messages'][60042];
	} catch (Exception $e) {
		$output['code'] = 500;
		$output['status'] = 'fail';
		$output['message'] = $GLOBALS['messages'][90003];
	}
	return $output;
}

/**
 * Function to edit UNetLab user.
 *
 * @param	PDO		$db					PDO object for database connection
 * @param	string	$user				UNetLab user
 * @param	Array	$p					Parameters
 * @return  Array                       Return code (JSend data)
 */
function apiEditmatch($db, $user, $p) {
	// TODO missing try/catch
	// TODO need to check all parameters
	if (empty($user)) {
		// User not found
		$output['code'] = 404;
		$output['status'] = 'fail';
		$output['message'] = $GLOBALS['messages'][60039];
		return $output;
	}
	/*if($p['oldname'] == $p['name'])
	{
		$output['code'] = 200;
		$output['status'] = 'success';
		$output['message'] = $GLOBALS['messages'][60042];		
		return $output;
	}*/
    $olduname=htmlentities($p['oldname']);
	$uname=htmlentities($p['name']);
	//
	$usernameok = true;
	try {
		$query = 'SELECT COUNT(*) as rows FROM users WHERE username = :username;';
		
		$statement = $db -> prepare($query);
		$statement -> bindParam(':username', $uname, PDO::PARAM_STR);
		
		$statement -> execute();
		$result = $statement -> fetch();
		if ($result['rows'] > 0) {
			$usernameok = false;
			}
		}
		catch (Exception $e) {
		$usernameok = false;
	}
	if($usernameok == false)
	{
	$output['code'] = 401;
	$output['status'] = 'fail';
	$output['message'] = "和用户名重复";
	return $output;
	}
	$usernameok = true;
	try {
		$query = 'SELECT COUNT(*) as rows FROM class WHERE name = :username;';
		
		$statement = $db -> prepare($query);
		$statement -> bindParam(':username', $uname, PDO::PARAM_STR);
		
		$statement -> execute();
		$result = $statement -> fetch();
		if ($result['rows'] > 0) {
			$usernameok = false;
			}
		}
		catch (Exception $e) {
		$usernameok = false;
	}
	if($usernameok == false)
	{
	$output['code'] = 401;
	$output['status'] = 'fail';
	$output['message'] = "和班级名重复";
	return $output;
	}
	$usernameok = true;
	try {
		$query = 'SELECT COUNT(*) as rows FROM `match` WHERE name = :username and id <> :id;';
		
		$statement = $db -> prepare($query);
		$statement -> bindParam(':username', $uname, PDO::PARAM_STR);
		$statement -> bindParam(':id', $user, PDO::PARAM_INT);
		$statement -> execute();
		$result = $statement -> fetch();
		if ($result['rows'] > 0) {
			$usernameok = false;
			}
		}
		catch (Exception $e) {
		$usernameok = false;
	}
	if($usernameok == false)
	{
	$output['code'] = 401;
	$output['status'] = 'fail';
	$output['message'] = "和比赛名重复";
	return $output;
	}
	//
	$query = 'UPDATE matchreferee SET matchname = :name WHERE matchid = :id;';
	$statement = $db -> prepare($query);	
	$statement -> bindParam(':name', $uname, PDO::PARAM_STR);
	$statement -> bindParam(':id', $user, PDO::PARAM_INT);
	
	$statement -> execute();
	
	$query = 'UPDATE matchplayer SET matchname = :name WHERE matchid = :id;';
	$statement = $db -> prepare($query);
	$statement -> bindParam(':name', $uname, PDO::PARAM_STR);
	$statement -> bindParam(':id', $user, PDO::PARAM_INT);
	$statement -> execute();
	
	$query = 'UPDATE `match` SET name = :name,sdate = :sdate, didian=:didian,leixin=:leixin,lianxiren=:lianxiren,bstime=:bstime,beizhu=:beizhu WHERE id = :id;';
	$statement = $db -> prepare($query);
	$statement -> bindParam(':name', $uname, PDO::PARAM_STR);
	$statement -> bindParam(':id', $user, PDO::PARAM_INT);
	$statement -> bindParam(':sdate',$p['sdate'] , PDO::PARAM_STR);	
	//
	$statement -> bindParam(':didian',$p['didian'] , PDO::PARAM_STR);
	$statement -> bindParam(':leixin',$p['leixin'] , PDO::PARAM_STR);
	$statement -> bindParam(':lianxiren',$p['lianxiren'] , PDO::PARAM_STR);
	$statement -> bindParam(':bstime',$p['bstime'] , PDO::PARAM_INT);
	$statement -> bindParam(':beizhu',$p['beizhu'] , PDO::PARAM_STR);
	
	//
	$statement -> execute();
	$output['code'] = 200;
	$output['status'] = 'success';
	$output['message'] = $GLOBALS['messages'][60042];
	if($p['oldname'] !== $p['name'])
	{
		$output = apiEditFolder($db, "/".$p['oldname'], "/".$p['name'], $p['name']);
	}
	
	return $output;
}

/**
 * Function to add a UNetLab user.
 *
 * @param	PDO		$db					PDO object for database connection
 * @param	Array	$p					Parameters
 * @return  Array                       Return code (JSend data)
 */
function apiAddmatch($db, $p) {
	// TODO need to check all parameters
	if (!isset($p['name']) ) {
		// Username not set
		$output['code'] = 400;
		$output['status'] = 'fail';
		$output['message'] = $GLOBALS['messages'][60043];
		return $output;
	}
    $uname=htmlentities($p['name']);
	//
	$usernameok = true;
	try {
		$query = 'SELECT COUNT(*) as rows FROM users WHERE username = :username;';
		
		$statement = $db -> prepare($query);
		$statement -> bindParam(':username', $uname, PDO::PARAM_STR);
		$statement -> execute();
		$result = $statement -> fetch();
		if ($result['rows'] > 0) {
			$usernameok = false;
			}
		}
		catch (Exception $e) {
		$usernameok = false;
	}
	if($usernameok == false)
	{
	$output['code'] = 401;
	$output['status'] = 'fail';
	$output['message'] = "和用户名重复";
	return $output;
	}
	$usernameok = true;
	try {
		$query = 'SELECT COUNT(*) as rows FROM class WHERE name = :username;';
		
		$statement = $db -> prepare($query);
		$statement -> bindParam(':username', $uname, PDO::PARAM_STR);
		$statement -> execute();
		$result = $statement -> fetch();
		if ($result['rows'] > 0) {
			$usernameok = false;
			}
		}
		catch (Exception $e) {
		$usernameok = false;
	}
	if($usernameok == false)
	{
	$output['code'] = 401;
	$output['status'] = 'fail';
	$output['message'] = "和班级名重复";
	return $output;
	}
	$usernameok = true;
	try {
		$query = 'SELECT COUNT(*) as rows FROM `match` WHERE name = :username;';
		
		$statement = $db -> prepare($query);
		$statement -> bindParam(':username', $uname, PDO::PARAM_STR);
		$statement -> execute();
		$result = $statement -> fetch();
		if ($result['rows'] > 0) {
			$usernameok = false;
			}
		}
		catch (Exception $e) {
		$usernameok = false;
	}
	if($usernameok == false)
	{
	$output['code'] = 401;
	$output['status'] = 'fail';
	$output['message'] = "和比赛名重复";
	return $output;
	}
	//
	$query = 'INSERT INTO `match` (name,sdate,cdate,didian,leixin,lianxiren,bstime,beizhu) VALUES (:name,:sdate,now(),:didian,:leixin,:lianxiren,:bstime,:beizhu);';
	try {
		$statement = $db -> prepare($query);
		$statement -> bindParam(':name', $uname, PDO::PARAM_STR);
		$statement -> bindParam(':sdate',$p['sdate'] , PDO::PARAM_STR);
		
		$statement -> bindParam(':didian',$p['didian'] , PDO::PARAM_STR);
		$statement -> bindParam(':leixin',$p['leixin'] , PDO::PARAM_STR);
		$statement -> bindParam(':lianxiren',$p['lianxiren'] , PDO::PARAM_STR);
		$statement -> bindParam(':bstime',$p['bstime'] , PDO::PARAM_INT);
		$statement -> bindParam(':beizhu',$p['beizhu'] , PDO::PARAM_STR);
		$statement -> execute();
		$output['code'] = 201;
		$output['status'] = 'success';
		$output['message'] = $GLOBALS['messages'][60042];
		//mkdir(BASE_LAB.'/'.$p['username']);
	} catch (Exception $e) {
		$output['code'] = 500;
		$output['status'] = 'fail';
		$output['message'] = $GLOBALS['messages'][60045];
		return $output;
	}	
	$output = apiAddFolder($db, $p['name'], $p['name'], '/', 'admin');
	return $output;
}
?>
